package com.mall.struts.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mall.ibatis.bean.Method;
import com.mall.ibatis.bean.ReturnMessage;
import com.mall.service.FunctionMethodService;
import com.mall.service.FunctionService;

public class FunctionMethodAction extends Action{
	
	private static final FunctionMethodService functionMethodService =  FunctionMethodService.getInstance();
	private static final FunctionService functionService = FunctionService.getInstance();
	
	@Override
	public ActionForward execute(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws Exception{
		String type = request.getParameter("type");
		if("list".equals(type)){
			return list(mapping,form,request,response);
			
		}else if("methodLoad".equals(type)){
			return methodLoad(mapping,form,request,response);
			
		}else if("updateFunMethod".equals(type)){
			return updateFunMethod(mapping,form,request,response);
			
		}else if("loading".equals(type)){
			return loading(mapping,form,request,response);
			
		}else if("methodIsChoose".equals(type)){
			return methodIsChoose(mapping,form,request,response);
			
		}
		return null;
	}

	//查询已经勾选过的方法
	private ActionForward methodIsChoose(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception{
		String funId = request.getParameter("funId");
		String xml = functionMethodService.methodIsChoose(funId);
		
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print(xml);
		out.flush();
		out.close();
		return null;
	}

	//把方法和功能多对一得关系保存在fun_method表中
	private ActionForward updateFunMethod(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		String methodId = request.getParameter("methodId");
		
		ReturnMessage message = functionMethodService.updateFunMethod(id,methodId);
		String xml = message.toXMLString();
		response.setContentType("text/xml;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(xml);
		out.flush();
		out.close();
		return null;
	}

	//查询方法method表中有哪些方法，并加载到页面的选择框中
	private ActionForward methodLoad(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		List<Method> methods = functionMethodService.selectAllMethod();
		request.setAttribute("methodList",methods);
		return mapping.findForward("methodList");
	}

	//方法列表的加载.在功能jf_funmenu数据库表中查询出所有的方法,
	private ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		String xml = functionMethodService.selectAllFunctionMethod().toXML();

		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print(xml);
		out.flush();
		out.close();
		return null;
	}
	
	private ActionForward loading(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		functionService.methodFilter(request,response);
		return mapping.findForward("loading");
	}
}
